<?php 
namespace app\index\validate;
use think\Validate;
use think\Db;
/**
 * Created by Submint Text.
 * User: hetaoren<452649510@qq.com>
 * Date: 2017/03/10
 * Explain: 职员验证器
 */
class Staffs extends Validate{
	protected $rule = [
	    ['loginName'  ,'require|max:20|checkLoginName:1','请输入登录账号|登录账号不能超过20个字符'],
	    ['loginPwd'  ,'require|min:6','请输入登录密码|登录密码不能少于6个字符'],
        ['staffName'  ,'require|max:60','请输入职员名称|职员名称不能超过20个字符'],
        ['staffPhone'  ,'require|checkStaffPhone:1','请输入职员手机号码'],
        ['staffNo'  ,'require|checkStaffNo:1','请输入职员编号'],
        ['staffRoleId'  ,'require|gt:0','请选择职员角色|请选择职员角色'],
        ['staffStatus','require|in:0,1','请选择账号状态|无效的账号状态值']
    ];

    protected $scene = [
        'add'   =>  ['loginName','loginPwd','staffName','staffPhone','staffStatus','staffNo','staffRoleId'],
        'edit'  =>  ['staffName','staffPhone','staffStatus','staffNo','staffRoleId'],
    ]; 
    
    protected function checkLoginName($value){
        $where = [];
        $where['dataFlag'] = 1;
        $where['loginName'] = $value;
        $rs = Db::name('staffs')->where($where)->count();
        return ($rs==0)?true:'该登录账号已存在';
    }
    protected function checkStaffNo($value){
        $where = [];
        $where['dataFlag'] = 1;
        $where['staffNo'] = $value;
        $staffId = Input('post.staffId/d',0);
        if($staffId)$where['staffId'] = ['neq',$staffId];
        $rs = Db::name('staffs')->where($where)->count();
        return ($rs==0)?true:'该职员编号已存在';
    }
    
    protected function checkStaffPhone($value){
        if(!HTRIsPhone($value))return '手机号码格式错误';
        $where = [];
        $where['dataFlag'] = 1;
        $where['staffPhone'] = $value;
        $staffId = Input('post.staffId/d',0);
        if($staffId)$where['staffId'] = ['neq',$staffId];
        $rs = Db::name('staffs')->where($where)->count();
        return ($rs==0)?true:'该手机号码已存在';
    }
}